package com.example.controller;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.pojo.PageData;
import com.example.pojo.ResultBean;
import com.example.pojo.Waste;
import com.example.pojo.WasteType;
import com.example.service.IWasteService;
import com.example.vo.WasteVo;
import javafx.print.PaperSource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author lyj
 * @since 2023-02-16
 */
@RestController
@RequestMapping("/waste")
public class WasteController {
    @Autowired
    private IWasteService wasteService;

    /**
     * 分页条件查询
     * @param pageData
     * @param map
     * @return
     */
    @GetMapping("/list")
    public ResultBean list(PageData pageData,
                           @RequestParam Map<String, Object> map) {
        Page<WasteVo> page = new Page<>(pageData.getPageNum(), pageData.getPageSize());
        wasteService.queryByCondition(page,map);
        return ResultBean.success(page);
    }

    /**
     * 新增或修改
     * @param waste
     * @return
     */
    @PostMapping("saveOrUpdate")
    public ResultBean saveOrUpdate(@RequestBody Waste waste) {
        System.out.println("新增或修改的数据：" + waste);
        boolean rs = wasteService.saveOrUpdate(waste);
        return rs ? ResultBean.success() : ResultBean.error();
    }

    /**
     * 根据ID删除
     * @param id
     * @return
     */
    @DeleteMapping("delete/{id}")
    public ResultBean delete(@PathVariable("id") Long id) {
        System.out.println("要删除的id：" + id);
        boolean rs = wasteService.removeById(id);
        return rs ? ResultBean.success() : ResultBean.error();
    }

    /**
     * 根据wasteTypeID查询waste集合
     * @param id
     * @return
     */
    @GetMapping("queryByWasteTypeId/{id}")
    public ResultBean queryByWasteTypeId(@PathVariable("id") Long id){
        List<Waste> list = wasteService.lambdaQuery().eq(Waste::getParentId, id).list();
        return ResultBean.success(list);
    }

}
